package com.ruoyi.car.controller.admin.carBsRegulations;

import com.github.pagehelper.PageInfo;
import com.ruoyi.car.controller.admin.carBsRegulations.vo.CarBsRegulationsAddReq;
import com.ruoyi.car.controller.admin.carBsRegulations.vo.CarBsRegulationsEditReq;
import com.ruoyi.car.controller.admin.carBsRegulations.vo.CarBsRegulationsResp;
import com.ruoyi.car.controller.admin.model.vo.CarModelAddReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelEditReq;
import com.ruoyi.car.controller.admin.model.vo.CarModelResp;
import com.ruoyi.car.entity.CarBsRegulations;
import com.ruoyi.car.entity.CarModel;
import com.ruoyi.car.mapstruct.CarBsRegulationsTo;
import com.ruoyi.car.mapstruct.CarModelConvert;
import com.ruoyi.car.service.CarBsRegulationsService;
import com.ruoyi.car.service.CarModelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageReq;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author：流冬
 * @Package：com.ruoyi.car.controller.admin.carBsRegulations
 * @Project：RuoYi-Vue
 * @name：CarBsRegulationsController
 * @Date：2023/6/5 21:23
 * @Filename：CarBsRegulationsController
 * @Description
 */
@Api(tags = "管理后台-业务归属")
// springmvc的 @Controller+@ResponseBody
@RestController
// springmvc注解
@RequestMapping(value = "/car/carBsRegulations")
public class CarBsRegulationsController {
    @Autowired
    private CarBsRegulationsService carBsRegulationsService;


    // spring security权限
    @PreAuthorize(value = "@ss.hasPermi('car:carBsRegulations:page')")
    // springmvc  get请求注解
    @GetMapping(value = "/page")
    // swagger3 的注解
    // @Operation(summary = "获取分页列表")
    @ApiOperation(value = "获取分页列表")
    public TableDataInfo list(PageReq req){
        PageInfo<CarBsRegulations> pageInfo = this.carBsRegulationsService.search(req);

        //总记录数
        long count= pageInfo.getTotal();
        //转换完成以后的结果
        List<CarBsRegulationsResp> list= CarBsRegulationsTo.INSTANCE.convert(pageInfo.getList());

        TableDataInfo tableDataInfo=new TableDataInfo(list,count);
        return tableDataInfo;


    }

    @PreAuthorize(value = "@ss.hasPermi('car:carBsRegulations:add')")
    @Log(title = "业务归属",businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid CarBsRegulationsAddReq req){
        CarBsRegulations convert = CarBsRegulationsTo.INSTANCE.convert(req);
        boolean save = this.carBsRegulationsService.save(convert);
        return save? R.ok(): R.fail();
    }

    @PreAuthorize(value = "@ss.hasPermi('car:carBsRegulations:query')")
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/{id}")
    public R<CarBsRegulationsResp> findById(@PathVariable(value = "id") Long id){
        CarBsRegulations db = this.carBsRegulationsService.getById(id);
        if(db==null){
            //国际化 i18n 中查询
            String msg= MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarBsRegulationsResp convert = CarBsRegulationsTo.INSTANCE.convert(db);
        return R.ok(convert);
    }

    @PreAuthorize(value = "@ss.hasPermi('car:carBsRegulations:edit')")
    @Log(title = "业务归属",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改保存")
    @PutMapping
    public R  update(@RequestBody @Valid CarBsRegulationsEditReq req){
        CarBsRegulations db = this.carBsRegulationsService.getById(req.getId());
        if(db==null){
            //国际化 i18n 中查询
            String msg=MessageUtils.message("db.not.found");
            throw new ServiceException(msg);
        }

        CarBsRegulations convert = CarBsRegulationsTo.INSTANCE.convert(req);

        boolean save = this.carBsRegulationsService.updateById(convert);
        return save? R.ok(): R.fail();
    }
}
